﻿using System;
using System.Collections.Generic;

namespace Dotnet.TopApi
{
    /// <summary>
    /// 符合TOP习惯的纯字符串字典结构。
    /// </summary>
    public class TopDictionary : Dictionary<string, string>
    {
        private const string DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

        public TopDictionary() { }

        public TopDictionary(IDictionary<string, string> dictionary)
            : base(dictionary)
        { }

        /// <summary>
        /// 添加一个新的键值对。空键或者空值的键值对将会被忽略。
        /// </summary>
        /// <param name="key">键名称</param>
        /// <param name="value">键对应的值，目前支持：string, int, long, double, bool, DateTime类型</param>
        public void Add(string key, object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = null;
            }
            else if (value is string)
            {
                strValue = (string)value;
            }
            else if (value is Nullable<DateTime>)
            {
                Nullable<DateTime> dateTime = value as Nullable<DateTime>;
                strValue = dateTime.Value.ToString(DATE_TIME_FORMAT);
            }
            else if (value is Nullable<int>)
            {
                strValue = (value as Nullable<int>).Value.ToString();
            }
            else if (value is Nullable<long>)
            {
                strValue = (value as Nullable<long>).Value.ToString();
            }
            else if (value is Nullable<double>)
            {
                strValue = (value as Nullable<double>).Value.ToString();
            }
            else if (value is Nullable<bool>)
            {
                strValue = (value as Nullable<bool>).Value.ToString().ToLower();
            }
            else
            {
                strValue = value.ToString();
            }

            this.Add(key, strValue);
        }

        public new void Add(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                base.Add(key, value);
            }
        }

        public void AddAll(IDictionary<string, string> dict)
        {
            if (dict != null && dict.Count > 0)
            {
                IEnumerator<KeyValuePair<string, string>> kvps = dict.GetEnumerator();
                while (kvps.MoveNext())
                {
                    KeyValuePair<string, string> kvp = kvps.Current;
                    Add(kvp.Key, kvp.Value);
                }
            }
        }
        ///////////////////////
        public void AddHeaders(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                base.Add("Headers:" + key, value);
            }
        }
        public void AddHeadersContentType(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                base.Add("Headers:Content-Type",value);
            }
        }
        public string GetHeadersContentType()
        {
            return GetHeaders("Content-Type");
        }

        public void AddHeadersPwd(string key,string value)
        {
            if (!string.IsNullOrEmpty(value)&& !string.IsNullOrEmpty(key))
            {
                base.Add($"Headers:Pwd", $"{key}:{value}");
            }
        }
        public void GetHeadersPwd(out string key,out string value)
        {
            string tmp;
            key = "";value = "";
            base.TryGetValue("Headers:Pwd", out tmp);
            if (tmp == null) return;
            if (tmp.Contains(":"))
            {
                string[] split = tmp.Split(':');
                key = split[0];
                value = split[1];
            }
        }

        public void AddHeadersMethod(string name)
        {
            AddHeaders("METHOD", name);
        }
        public string GetHeadMethod()
        {
            return GetHeaders("METHOD");
        }

        public string GetHeaders(string key)
        {
            string value = "";
            base.TryGetValue("Headers:" + key, out value);
            return value;
        }

        public void AddBobyContent(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                base.Add("Body:Raw:JSON", value);
            }
        }
        public string GetBobyContent()
        {
            string value = "";
            base.TryGetValue("Body:Raw:JSON", out value);
            return value;
        }
        public IDictionary<string, string> GetHeadersParameters()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            foreach (string key in this.Keys)
            {
                if (key.Contains("HP"))
                {
                    string value = "";
                    bool ret = this.TryGetValue(key, out value);
                    if (ret == true) dict.Add(key.Replace("Headers:HP:", ""), value);
                }
            }
            return dict;
        }
    }
}
